package com.example.innovate.controller;

import com.example.innovate.bean.Result;
import com.example.innovate.domain.EnterpriseDO;
import com.example.innovate.service.EnterpriseService;
import com.example.innovate.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/api/enterprise")
@Api(tags = "企业信息接口")
public class EnterpriseController {

    @Autowired
    EnterpriseService enterpriseService;



    @DeleteMapping("/deleteEnterprise")
    @ApiOperation("删除企业信息")
    public Result deleteEnterprise(int id) throws Exception{
        EnterpriseDO enterpriseDO = enterpriseService.findById(id);
        if(enterpriseDO!=null){
            enterpriseService.deleteInfo(enterpriseDO.getEnId());
        }
        else{
            return new Result(400,"error","信息不存在",null);
        }
        return new Result(200,"success","删除成功",enterpriseDO);

    }

    @PostMapping("/updateEnterprise")
    @ApiOperation("修改企业信息")
    public Result updateEnterprise(@RequestBody EnterpriseDO enterpriseDO) throws Exception{
        EnterpriseDO enterpriseDO1 = enterpriseService.findById(enterpriseDO.getEnId());
        if(enterpriseDO1!=null){
            enterpriseService.updateInfo(enterpriseDO);
            return new Result(200,"success","修改成功",enterpriseDO);
        }
        else {
            return new Result(400,"error","修改信息不存在",null);
        }

    }

    @GetMapping("/findEnterprise")
    @ApiOperation("查找企业信息")
    public Result findEnterprise(String name,
                                   @RequestParam Integer pageSize,
                                   @RequestParam Integer pageNumber) throws Exception{
        List<EnterpriseDO> list = enterpriseService.findAll(name,pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);
    }

    //@UserLoginToken
    @PostMapping("/upload")
    @ApiOperation("上传二维码图片")
    public Result upload(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws Exception{
        EnterpriseDO enterpriseDO = enterpriseService.findById(id);
        String fileName = FileUtil.upload(file);
        String filePath = "/root/file/";
        File file1 = new File(filePath + fileName);
        // 如果文件存在
        String s = "";
        if (file1.exists()) {
            s = "http://42.193.219.136:8080/images/"+fileName;
        }
        else {
            s = "文件上传失败";
        }
        if(enterpriseDO!=null){
            if(enterpriseDO.getEnQrCode()!=null){
                String deleteName = enterpriseDO.getEnQrCode();
                filePath = "/root/file/" ;//C:\\file\\
                File filePast = new File(filePath + deleteName);
                if (filePast.isFile() && filePast.exists()) {
                    filePast.delete();
                }
            }
            enterpriseDO.setEnQrCode(s);
            enterpriseService.updateInfo(enterpriseDO);
            return new Result(200,"success","添加成功",enterpriseDO);
        }
        else{
            EnterpriseDO enterpriseDO1 = new EnterpriseDO();
            enterpriseDO1.setEnQrCode(s);
            enterpriseService.addInfo(enterpriseDO1);
            return new Result(200,"success","添加成功",enterpriseDO1);
        }
    }
    @PostMapping("/addEnterprise")
    @ApiOperation("添加企业信息")
    public Result addEnterprise(@RequestBody EnterpriseDO enterpriseDO) throws Exception{
        EnterpriseDO enterpriseDO1 = enterpriseService.findByPicture(enterpriseDO.getEnQrCode());
        if (enterpriseDO1!=null){
            enterpriseDO.setEnId(enterpriseDO1.getEnId());
            enterpriseDO.setEnQrCode(enterpriseDO1.getEnQrCode());
            enterpriseService.updateInfo(enterpriseDO);
        }
        else{
            enterpriseService.addInfo(enterpriseDO);
        }
        return new Result(200,"success","添加成功",enterpriseDO);
    }

}
